﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace winformDoDragDrop
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            listBox1.Items.Add(1);
            listBox1.Items.Add(2);
            listBox1.Items.Add(3);
            //richTextBox1.AllowDrop = true;
            //richTextBox1.DragEnter += richTextBox1_DragEnter;
            //richTextBox1.DragDrop += richTextBox1_DragDrop;

            richTextBox1.AllowDrop = true;

            richTextBox1.DragEnter += new DragEventHandler(richTextBox1_DragEnter);
            richTextBox1.DragDrop += new DragEventHandler(richTextBox1_DragDrop);
        }

        private void listBox1_MouseDown(object sender, MouseEventArgs e)
        {
            // Determines which item was selected.
            ListBox lb = ((ListBox)sender);
            Point pt = new Point(e.X, e.Y);
            int index = lb.IndexFromPoint(pt);

            // Starts a drag-and-drop operation with that item.
            if (index >= 0)
            {
                lb.DoDragDrop(lb.Items[index].ToString(), DragDropEffects.Link);
                int i = 0;
                //lb.DoDragDrop(lb.Items[index].ToString(), DragDropEffects.Copy);
            }
        }
        private void richTextBox1_DragEnter(object sender, DragEventArgs e)
        {
            // If the data is text, copy the data to the RichTextBox control.
            //if (e.Data.GetDataPresent("Text"))
                e.Effect = DragDropEffects.Link;
        }

        private void richTextBox1_DragDrop(object sender, DragEventArgs e)
        {
            // Loads the file into the control. 
            richTextBox1.LoadFile((String)e.Data.GetData("Text"), System.Windows.Forms.RichTextBoxStreamType.RichText);
        }
    }
}
